package com.pms.init.base;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.manager.workflow.WFCondition;
import com.espirit.eap.pagelayout.DetailLayout;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.eap.pagelayout.SectionElement;
@Component
public class WFConditionBuilder extends ModelBuilderSupport {

	private static final String FIELD_TRIGGER = "acceptTrigger";
	private static final String FIELD_CONDITION = "triggerCondition";
	
	
	@Override
	public boolean init() {
		return super.setClass(WFCondition.class, "Condition", "条件") ;
	}
	
	@Override
	public void buildFields() {
		addModelField(FIELD_TRIGGER, "Accept Req", "批准需求");
		addModelField(FIELD_CONDITION, "Condition", "条件");
	}
	
	@Override
	public void prepareLayoutElements() {
	}
	
	@Override
	protected void preBuildMiscDatas() {
		ModelField mfTrigger = getModelField(FIELD_TRIGGER);
		
		ModelField mfCondition = getModelField(FIELD_CONDITION);
		
		addListField(mfTrigger,mfCondition);
		addDetailField(mfTrigger,mfCondition);
		DetailLayout detailLayout = createDetailLayout(model, null, false);
		FlatSection fs = detailLayout.createFlatSection();
		fs.setNamePrefix("wfCondition");
		elementService.addSectionElement(fs, 
				convertFieldToElement(new ModelField[] {
				mfTrigger,mfCondition
		}, false));
		updateObject(model);
	}
	
	@Override
	protected void afterBuilderLayout() {
		DetailLayout bs = model.getDefaultDetailLayout();
		SectionElement seCondition = bs.findSectionElement(FIELD_CONDITION);
		seCondition.setTagName("textarea");
		
		SectionElement seTrigger = bs.findSectionElement(FIELD_TRIGGER);
		Map<String, String> exDateTimeTypeProperties = new HashMap<String, String>();
		exDateTimeTypeProperties.put("collection", "fn.service('espirit.eap.manager.workflow.wFInstance').triggerItems");
		seTrigger.setExProperty(exDateTimeTypeProperties);
		seTrigger.setTagName("radio");
		
		updateObject(model);
		
		super.afterBuilderLayout();
		
	}
	
	@Override
	protected Integer getDetailLayoutSectionColsPerRow() {
		return 1;
	}
}
